<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
		include './'.$class_name . '.php';
	}
	$result = array();
	$productId=$_GET['productId'];
	$quantity = $_GET['quantity'];
	$status = true;	

	$cart = NULL;
	if(!isset($_SESSION['cart'])){
		$result['#success'] = false;
		$status = false;
	}else {
		$cart = $_SESSION['cart'];
	}
	
	if($status && (!is_numeric($productId) || !is_numeric($quantity))) {
		$status = false;
	}
	
	
	
	
	if ($status && (isset($cart->getOrderList()[$productId]))){
		$order = $cart->getOrderList()[$productId];
		if($quantity < 1){
			$quantity = 1;
		}
		$order->setQuantity($quantity);
		$result['#success'] = true;
	} else {
		$result['#success'] = false;
	}

	if($status){
		header("Location: shoppingcart.php");
	} else {
		echo json_encode($result);
	}
	ob_end_flush();
?>